home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / FragmentLinker / FragmentLinker.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  5.1 KB  |  131 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: FragmentLinker.fx
  3. //
  4. // The effect file for the FragmentLinker sample.  
  5. // 
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8.  
  9.  
  10. //--------------------------------------------------------------------------------------
  11. // Global variables
  12. //--------------------------------------------------------------------------------------
  13. float4 g_vMaterialAmbient = float4( 0.3f, 0.3f, 0.3f, 1.0f );       // Material's ambient color
  14. float4 g_vMaterialDiffuse = float4( 0.6f, 0.6f, 0.6f, 1.0f );       // Material's diffuse color
  15.  
  16. float4   g_vLightColor = float4( 1.0f, 1.0f, 1.0f, 1.0f );        // Light color
  17. float3   g_vLightPosition = float3( 0.0f, 5.0f, -5.0f );          // Light position
  18.  
  19. texture  g_MeshTexture;             // Color texture for mesh
  20.  
  21. float     g_fTime;                    // App's time in seconds
  22. float4x4 g_mWorld;                  // World matrix
  23. float4x4 g_mWorldViewProjection;    // World * View * Projection matrix
  24.  
  25. VertexShader MyVertexShader;        // Vertex shader set by the application
  26.  
  27.  
  28. //--------------------------------------------------------------------------------------
  29. // Texture samplers
  30. //--------------------------------------------------------------------------------------
  31. sampler MeshTextureSampler = 
  32. sampler_state
  33. {
  34.     Texture = <g_MeshTexture>;    
  35.     MipFilter = LINEAR;
  36.     MinFilter = LINEAR;
  37.     MagFilter = LINEAR;
  38. };
  39.  
  40.  
  41. //--------------------------------------------------------------------------------------
  42. // Name: Projection
  43. // Type: Vertex Shader Fragment
  44. // Desc: Projection transform
  45. //--------------------------------------------------------------------------------------
  46. void Projection( float4 vPosObject: POSITION,
  47.                  float3 vNormalObject: NORMAL,
  48.                  float2 vTexCoordIn: TEXCOORD0,
  49.                  out float4 vPosWorld: r_PosWorld,
  50.                  out float3 vNormalWorld: r_NormalWorld,
  51.                  out float4 vPosProj: POSITION,
  52.                  out float2 vTexCoordOut: TEXCOORD0,
  53.                  uniform bool bAnimate
  54.                )
  55. {
  56.     // Optional vertex animation
  57.     if( bAnimate )
  58.         vPosObject.x *= (1 + sin( g_fTime )/2);
  59.         
  60.     // Transform the position into world space for lighting, and projected space
  61.     // for display
  62.     vPosWorld = mul( vPosObject, g_mWorld );
  63.     vPosProj = mul( vPosObject, g_mWorldViewProjection );
  64.     
  65.     // Transform the normal into world space for lighting
  66.     vNormalWorld = mul( vNormalObject, (float3x3)g_mWorld );
  67.     
  68.     // Pass the texture coordinate
  69.     vTexCoordOut = vTexCoordIn;
  70. }
  71. vertexfragment ProjectionFragment_Animated = compile_fragment vs_1_1 Projection( true );
  72. vertexfragment ProjectionFragment_Static = compile_fragment vs_1_1 Projection( false );
  73.  
  74.  
  75. //--------------------------------------------------------------------------------------
  76. // Name: Ambient
  77. // Type: Vertex Shader Fragment
  78. // Desc: Ambient scene lighting
  79. //--------------------------------------------------------------------------------------
  80. void Ambient( out float4 vColor: COLOR0 )
  81. {
  82.     // Compute the ambient component of illumination
  83.     vColor = g_vLightColor * g_vMaterialAmbient;
  84. }
  85. vertexfragment AmbientFragment = compile_fragment vs_1_1 Ambient();
  86.  
  87.  
  88. //--------------------------------------------------------------------------------------
  89. // Name: AmbientDiffuse
  90. // Type: Vertex Shader Fragment
  91. // Desc: Ambient scene lighting
  92. //--------------------------------------------------------------------------------------
  93. void AmbientDiffuse( float3 vPosWorld: r_PosWorld,
  94.                      float3 vNormalWorld: r_NormalWorld,
  95.                      out float4 vColor: COLOR0 )
  96. {  
  97.     // Compute the light vector
  98.     float3 vLight = normalize( g_vLightPosition - vPosWorld );
  99.     
  100.     // Compute the ambient and diffuse components of illumination
  101.     vColor = g_vLightColor * g_vMaterialAmbient;
  102.     vColor += g_vLightColor * g_vMaterialDiffuse * saturate( dot( vLight, vNormalWorld ) );
  103. }
  104. vertexfragment AmbientDiffuseFragment = compile_fragment vs_1_1 AmbientDiffuse();
  105.  
  106.  
  107. //--------------------------------------------------------------------------------------
  108. // Name: ModulateTexture
  109. // Type: Pixel Shader
  110. // Desc: Multiply the interpolated vertex color by the texture
  111. //--------------------------------------------------------------------------------------
  112. void ModulateTexture( float4 vColorIn: COLOR0,
  113.                       float2 vTexCoord: TEXCOORD0,
  114.                       out float4 vColorOut: COLOR0 )
  115. {  
  116.     // Sample and modulate the texture
  117.     vColorOut = vColorIn * tex2D( MeshTextureSampler, vTexCoord );
  118. }
  119.  
  120.  
  121. //--------------------------------------------------------------------------------------
  122. // Techniques
  123. //--------------------------------------------------------------------------------------
  124. technique RenderScene
  125. {
  126.     pass P0
  127.     {
  128.         VertexShader = <MyVertexShader>;    
  129.         PixelShader = compile ps_1_1 ModulateTexture();    
  130.     }
  131. }